﻿using Super.Admin.EFCore;
using Super.Admin.Entities;
using Super.Admin.IServices;

namespace Super.Admin.Services
{
    /// <summary>
    /// 权限服务实现类
    /// </summary>
    public class PermissionService : BaseService, IPermissionService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">操作的数据库上下文</param>
        public PermissionService(SuperDataBaseDbContext context) : base(context) { }
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public IReadOnlyCollection<PermissionEntity> GetPermissionMenus()
        {
            return Query<PermissionEntity>(p => p.Type != 2).ToList();
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns>权限实体集合</returns>
        public IReadOnlyCollection<PermissionEntity> GetPermissions()
        {
            return Set<PermissionEntity>().OrderBy(p => p.Sort).ToList();
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <param name="menutype">权限类型</param>
        /// <returns>权限实体集合</returns>
        public IReadOnlyCollection<PermissionEntity> GetPermissions(int menutype)
        {
            return Query<PermissionEntity>(p => p.Type == menutype).ToList();
        }
        /// <summary>
        /// 获取权限通过角色Ids
        /// </summary>
        /// <param name="roleIdList">角色权限Id</param>
        /// <returns>权限实体集合</returns>
        public IReadOnlyCollection<PermissionEntity> GetPermissionsByUserRoleIds(IEnumerable<int> roleIds)
        {
            if (roleIds == null || roleIds.Count() == 0)
            {
                return new List<PermissionEntity>();
            }
            // 获取所有角色的权限
            var rolePermissionEntities = Query<RolePermissionEntity>(rp => roleIds.Contains(rp.RoleId)).ToList();
            List<int> permissionIds = new List<int>();
            foreach (var permission in rolePermissionEntities)
            {
                permissionIds.Add(permission.PermissionId);
            }
            return Query<PermissionEntity>(p => permissionIds.Contains(p.Id) && p.IsActive == 0).OrderBy(p => p.Sort).ToList();
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="permission">新增的权限实体</param>
        public void CreatePermission(PermissionEntity permission)
        {
            if (permission == null)
            {
                throw new ArgumentNullException(nameof(permission), "菜单不能为空");
            }
            PermissionEntity existMenu = Query<PermissionEntity>(m => m.Id == permission.Id).ToList().FirstOrDefault();
            if (existMenu != null)
            {
                throw new Exception($"存在当前菜单：【{permission.Name}】");
            }
            permission.Id = Query<PermissionEntity>(x => true).Max(m => m.Id) + 1;
            Insert<PermissionEntity>(permission);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="permissionEntity">需要修改的权限实体</param>
        public void UpdatePermission(PermissionEntity permissionEntity)
        {
            Update<PermissionEntity>(permissionEntity);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="permissionId">权限ID</param>
        public void DeletePermission(int id)
        {
            // 删除此菜单下的所有的子菜单
            Delete<PermissionEntity>(id);
            var childEntity = Query<PermissionEntity>(p => p.ParentId == id).FirstOrDefault();
            if (childEntity != null)
            {
                DeletePermission(childEntity.Id);
            }
            base.Commit(); // 提交事务
        }
    }
}
